package mainService.services;

import java.util.ArrayList;

import mainService.HTTPService;
import mainService.ServiceParams;
import mainService.data.IpClient;
import mainService.data.UsersConnected;


/**
 * Servizio di Inserimento di un log
 * 
 * @author Daniel Procop, Mattia Grecchi
 */
public class InserisciLogService  extends WriterServices implements HTTPService {

	@Override
	public ArrayList<String> getServiceResult(ServiceParams params) {
		
		String data = params.getValue("data");
		int ore;
		try {
			ore = Integer.parseInt(params.getValue("ore"));
		} catch (Exception e) {
			ore = 0;
		}
		String task = params.getValue("task");
		String descrizione = params.getValue("description");
		String operatore = UsersConnected.getUsersConnected().getUtente(IpClient.getSingleton().getIp()).getUsername();
		int id_project = UsersConnected.getUsersConnected().getUtente(IpClient.getSingleton().getIp()).getId_project();
	

			String cmd = "INSERT INTO logs (id_task, data, hours, username, description) VALUES "
					+ "('"
					+ task
					+ "', '"
					+ data
					+ "', '"
					+ ore
					+ "', '"
					+ operatore 
					+ "', '"
					+ descrizione + "')";
			boolean var=updateDb(cmd);
			if (!var) {// inserimento non corretto
				params.addParam("file", "inserimentoLogFallito.html");
			} else {
				cmd= "Update tasks SET worked_hours = worked_hours + "+ore+" where id_task ='"+task+"' AND id_project='"+id_project+"';";
				var=updateDb(cmd);
				if(!var){// aggiornamento ore non corretto
					params.addParam("file", "inserimentoLogFallito.html");
				}else{
				params.addParam("file", "inserimentoLogRiuscito.html");
				}
			}
		return (new HttpFileServer()).getServiceResult(params);
	}
}
